package com.cn.dao;

import javax.sql.DataSource;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 * FUN: 管理事务
 *
 * @author sushy
 * @date 2016-3-23 
 */
public class TransactionManager extends DataSourceTransactionManager{

	private static final long serialVersionUID = 8255411883648948532L;
	private DefaultTransactionDefinition def = null;
	private TransactionStatus status = null;

	public TransactionManager(DataSource dataSource) {
		this(dataSource, TransactionDefinition.PROPAGATION_NEVER);
	}

	public TransactionManager(DataSource dataSource, int propagationBehavior) {
		super(dataSource);

		// 定义事务
		def = new DefaultTransactionDefinition();
		def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
		def.setPropagationBehavior(propagationBehavior);
	}

	/**
	 * 开始事务
	 */
	public void startTransaction() {
		status = getTransaction(def);
	}

	/**
	 * 提交事务
	 */
	public void commit() {
		if (status != null) {
			commit(status);
			status = null;
		}
	}

	/**
	 * 回滚事务
	 */
	public void rollback() {
		if (status != null) {
			rollback(status);
			status = null;
		}
	}


}
